package com.edu.etu.gui.checkit;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

    long rowIdSp;
    LinearLayout llMain;
    ImageButton btnAdd;
    Button btnTemplate;
    EditText input;
    SQLiteHelperList openHelperList;
    SQLiteDatabase db;
    ContentValues cv;
    Boolean f;
    private HashMap<View,Integer> mapLists = new HashMap<View,Integer>();
    SharedPreferences sPref;
    final String LOAD_VIEW = "view";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        llMain = (LinearLayout) findViewById(R.id.llMain);

        btnAdd=(ImageButton)findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(this);

        btnTemplate=(Button)findViewById(R.id.btnTemplate);
        btnTemplate.setOnClickListener(this);

        openHelperList = new SQLiteHelperList(getApplicationContext());
        db = openHelperList.getWritableDatabase();
        cv = new ContentValues();
        list();
        f=true;

        sPref = getPreferences(MODE_PRIVATE);
        String savedText = sPref.getString(LOAD_VIEW,"");
        Log.d("myLog", "TEST: "+savedText);
        if(savedText.equals(""))
        {
            AlertDialog.Builder adb = new AlertDialog.Builder(this);
            adb.setTitle(R.string.Welcome);
            adb.setMessage(R.string.message);
            adb.setNegativeButton(R.string.Ok, myClickListener2);
            adb.show();

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        //llMain.removeAllViews();
        //if(f==true)list();
        switch(v.getId())
        {
            case R.id.btnAdd:
                AlertDialog.Builder adb = new AlertDialog.Builder(this);
                input = new EditText(this);
                adb.setTitle(R.string.Enter);
                adb.setView(input);
                adb.setNegativeButton(R.string.no, myClickListener);
                adb.setPositiveButton(R.string.yes, myClickListener);
                adb.show();
            break;

            case R.id.btnTemplate:
                startActivity(new Intent(getBaseContext(), Template.class));
                break;
            default:
                for(Map.Entry<View,Integer> entry : mapLists.entrySet()) {
                    if(entry.getKey() == v)
                    {
                        Intent intent = new Intent(this, ViewListActivity.class);
                        intent.putExtra("ListId",entry.getValue());
                        startActivity(intent);
                    }
                }
                break;
        }

    }




    public void list()
    {
        llMain.removeAllViews();
        Cursor cList = db.query("Lists", null, null, null, null, null, null);
        if (cList.moveToFirst()) {
            do {
                LayoutInflater inflater = getLayoutInflater();
                View main_list_element = inflater.inflate(R.layout.main_list_element,null,false);
                Button open_list_button = (Button)main_list_element.findViewById(R.id.list_open_button);
                mapLists.put(open_list_button,cList.getInt(cList.getColumnIndex("_id")));
                open_list_button.setOnClickListener(this);
                open_list_button.setText(cList.getString(cList.getColumnIndex("nameLists")));
                llMain.addView(main_list_element);
            }
            while (cList.moveToNext());
        }
        cList.close();
    }
    DialogInterface.OnClickListener myClickListener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case Dialog.BUTTON_POSITIVE:
                    if(input.length()==0)input.setText("List");
                    cv.put(openHelperList.nameLists, input.getText().toString());
                    rowIdSp = db.insert(openHelperList.NameTableLists,null,cv);
                    Intent temp = new Intent(getBaseContext(), EdiListActivity.class);
                    temp.putExtra("ListId",(int)rowIdSp);
                    startActivity(temp);
                    break;
                case Dialog.BUTTON_NEGATIVE:

                    break;
            }
        }
    };

    DialogInterface.OnClickListener myClickListener2 = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            sPref = getPreferences(MODE_PRIVATE);
            SharedPreferences.Editor ed = sPref.edit();
            ed.putString(LOAD_VIEW, "check");
            ed.commit();
            Log.d("myLog", "PREF");
        }
    };
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        list();
        Log.d("myLog", "onRestoreInstanceState Main");
    }
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        list();
        Log.d("myLog", "onSaveInstanceState Main");
    }

}
